#include <stdlib.h>
#include "CUnit/Basic.h"
#include "util.c"

char *gFoo;

int setup(void)
{
        gFoo = strdup("i can has cheezburger");
        if(gFoo == NULL)
        {
                return 1;
        }

        return 0;
}

int teardown(void)
{
        free(gFoo);
        return 0;
}

void testPass(void)
{
        CU_ASSERT_STRING_EQUAL(gFoo, "i can has cheezburger");
}

void testFail(void)
{
        CU_ASSERT_STRING_EQUAL(gFoo, "no soup for you!");
}


/**
*  Checks for the correctness of the dictionary file
	1. Check whether the dictionary is empty
	2. Check words of the dictionary are of the same length and the number of words are within the limits 3 to 1000, comma-separated values
	3. Check whether the length is within the limits 2 to 100
	4. Check whether the file exists or not 
*/

void checkDictionary(char filename[]){ 
   int WORD_LENGTH_DICT;
   FILE *file = fopen ( filename, "r" );
   char *pch;
   int NUM_WORDS_DICT=0;

   // check if the file exists
   CU_ASSERT(file != NULL);

   if ( file != NULL )
   {
	  // @TODO change this limit to include the requirement MAX_WORDS = 1000, and MAX_WORD_LENGTH=100
      char line [ 128 ]; /* or other suitable maximum line size */
      fgets ( line, sizeof line, file ); /* read a line */
	
      int lattice_dimension = 3;
      CU_ASSERT(strlen(line)> lattice_dimension); // tests whether the line is empty or not	  
//	ltrim(rtrim(line));


      pch = strtok (line,",");
      WORD_LENGTH_DICT = strlen(pch);
      
      // check whether the wordlength is within the range (2,100)
      CU_ASSERT(strlen(WORD_LENGTH_DICT) > 2 && strlen(WORD_LENGTH_DICT) < 100 ) 
      while (pch != NULL){			
			NUM_WORDS_DICT++;
			pch = strtok (NULL,",");

			// check all the words are of the same length
			 CU_ASSERT(strlen(pch) == WORD_LENGTH_DICT);
      }	  

      // check whether the number of words in the dictionary are within 3 to 1000	  
      CU_ASSERT(NUM_WORDS_DICT >= 3 && NUM_WORDS_DICT < 1000); 	  
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
}



/**
*  Checks for the correctness of the input file
	1. Check whether the input is empty
	2. Check whether the input is of correct format
		All words should be of the same length
	 	New planes of the lattice should be in different lines
		This means we need to have N words each of length N in N Rows
		where N is the Lattice dimension	
	3. Check whether the lattice dimension is within the limits 4 to 1000 
	4. Check whether the file exists or not 
*/

void checkInput(char filename[]){
   int WORD_LENGTH=0;
   FILE *file = fopen ( filename, "r" );
   char *pch;
   int lattice_dimension = readLatticeDimension(); 
   int NUM_WORDS=0;
   int NUM_PLANES=0;


   // check whether the lattice_dimension is within the range [4, 1000)
   CU_ASSERT(lattice_dimension >=4 && lattice_dimension < 1000);
   
   // check if the file exists
   CU_ASSERT(file != NULL);

   if ( file != NULL )
   {
	  // @TODO change this limit to include the requirement MAX_WORDS = 1000, and MAX_WORD_LENGTH=100
      char line [ 128 ]; /* or other suitable maximum line size */
      fgets ( line, sizeof line, file ); /* read a line */
	
      int lattice_dimension = 3;
      CU_ASSERT(strlen(line)> lattice_dimension); // tests whether the line is empty or not	  
//	ltrim(rtrim(line));


      pch = strtok (line,",");
      WORD_LENGTH = strlen(pch);
      // check whether the wordlength is within the range [4,1000)
      CU_ASSERT(strlen(WORD_LENGTH) >= 4 && strlen(WORD_LENGTH) < 1000 ) ;
		
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
		 pch = strtok (line,",");
		 wordIndex = 0;
		 while (pch != NULL){
	                // check whether the wordlength is within the range [4,1000)
                        CU_ASSERT(strlen(pch)== WORD_LENGTH) ;
	       
			for(charIndex = 0; charIndex < LATTICE_DIMENSION; charIndex++)
			{
				lattice[charIndex][wordIndex][lineNo] = (char)pch[charIndex];
				//printf("%c\t", lattice[lineNo][charIndex][0]);
			}
			wordIndex++;
			pch = strtok (NULL,",");
		 }
	        
		// check whether the number of words in each line is equal to the wordlength or not. 
                CU_ASSERT(wordIndex == WORD_LENGTH);
	       
         //fputs ( line, stdout ); /* write the line */
		 NUM_PLANES++;
      }	  
  

      // check whether the number of planes is equal to wordlength	  
      CU_ASSERT(NUM_PLANES == WORD_LENGTH); 	  
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }

}

/**
*  Checks whether a particular word is present within the input 
*/

void checkDoSearch(){

}


/**
* Test the functioning of KMP algorithm for searching subsequence within a sequence
*/
void checkKMP(){
}




int main()
{
        CU_pSuite pSuite = NULL;

        if(CU_initialize_registry() != CUE_SUCCESS)
        {
                return CU_get_error();
        }

        pSuite = CU_add_suite("First Suite", setup, teardown);
        if(pSuite == NULL)
        {
                goto exit;
        }

        if(CU_add_test(pSuite, "Test Pass", testPass) == NULL)
        {
                goto exit;
        }

        if(CU_add_test(pSuite, "Test Fail", testFail) == NULL)
        {
                goto exit;
        }

        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();

exit:
        CU_cleanup_registry();
        return CU_get_error();
}

